\subsection{Fundamental lemma of calculus of variations}
Consider again the functional
\[
	F[y] = \int_\alpha^\beta f(x, y, y') \dd{x}
\]
where \(f\) is given, and \(f(\alpha, \wildcard, \wildcard)\) and \(f(\beta, \wildcard, \wildcard)\) are fixed.
Consider a small perturbation
\[
	y \mapsto y + \varepsilon \eta(x);\quad \eta(\alpha) = \eta(\beta) = 0
\]
In order to compute the functional for this new function, we first need an additional lemma.
\begin{lemma}[Fundamental lemma of calculus of variations]
	If \( g \colon [\alpha, \beta] \to \mathbb R \) is continuous on this interval, and is such that
	\[
		\forall \eta \text{ continuous}, \eta(\alpha) = \eta(\beta) = 0,\; \int_\alpha^\beta g(x) \eta(x) \dd{x} = 0
	\]
	Then
	\[
		\forall x \in (\alpha, \beta),\; g(x) \equiv 0
	\]
\end{lemma}
\begin{proof}
	Suppose that there exists a value \(\overline{x} \in (\alpha, \beta)\) such that \(g(\overline{x}) \neq 0\).
	Without loss of generality suppose that this value is positive.
	Then, by continuity, there exists a sub-interval \([x_1, x_2] \subset (\alpha, \beta)\) where \(g(x) > c\) for some positive real \(c\) in this sub-interval.
	So we will construct an \(\eta\) such that \(\eta > 0\) in \([x_1, x_2]\) and \(\eta = 0\) outside this interval, for example
	\[
		\eta(x) = \begin{cases}
			(x-x_1)(x_2-x) & x \in [x_1, x_2] \\
			0              & \text{otherwise}
		\end{cases}
	\]
	Then the integrand is non-negative everywhere, and is lower bounded by a positive number:
	\[
		\int_\alpha^\beta g(x) \eta(x) > c\int_{x_1}^{x_2} (x-x_1)(x_2-x)\dd{x} > 0
	\]
	So this leads to a contradiction.
\end{proof}
\begin{remark}
	We call such an \(\eta\) function a `bump function'.
	In general it is possible to construct a \(C^k\) bump function, e.g.
	\[
		\eta = \begin{cases}
			[(x-x_1)(x_2-x)]^{k+1} & x \in [x_1, x_2] \\
			0                      & \text{otherwise}
		\end{cases}
	\]
\end{remark}

\subsection{Euler--Lagrange equation}
Now, we can evaluate the original functional.
Using a Taylor expansion,
\begin{align*}
	F[y + \varepsilon \eta] & = \int_\alpha^\beta f(x, y+\varepsilon\eta, y'+\varepsilon\eta')                                        \\
	                        & = F[y] + \varepsilon\int_\alpha^\beta \qty(\pdv{f}{y}\eta + \pdv{f}{y'}\eta') \dd{x} + O(\varepsilon^2)
\end{align*}
For an extremum,
\[
	\eval{\dv{F}{\varepsilon}}_{\varepsilon = 0} = 0
\]
So we want the first order term to vanish, so
\[
	\varepsilon\int_\alpha^\beta \qty(\pdv{f}{y}\eta + \pdv{f}{y'}\eta') \dd{x} = 0
\]
Integrating by parts, we have
\begin{align*}
	0 & = \int_\alpha^\beta \qty(\pdv{f}{y}\eta - \dv{x} \qty(\pdv{f}{y'}\eta)) \dd{x} + \qty[\pdv{f}{y'}\eta]_\alpha^\beta \\
	  & = \int_\alpha^\beta \qty(\pdv{f}{y}\eta - \dv{x} \qty(\pdv{f}{y'}\eta)) \dd{x}                                      \\
	  & = \int_\alpha^\beta \underbrace{\qty(\pdv{f}{y} - \dv{x} \qty(\pdv{f}{y'}))}_{g(x)} \eta \dd{x}
\end{align*}
We can apply the lemma above, showing that a necessary condition for the optimum is
\[
	\dv{x} \qty(\pdv{f}{y'}) - \pdv{f}{y} = 0
\]
This is the Euler--Lagrange equation.
\begin{remark}
	Note that
	\begin{itemize}
		\item This can be seen as a second-order differential equation for \(y(x)\) with boundary conditions at \(\alpha\) and \(\beta\).
		\item The left hand side of the Euler--Lagrange equation is called a `functional derivative' of \(y\), and is written
		      \[
			      \fdv{F[y]}{y(x)}
		      \]
		      Sometimes, the notation
		      \[
			      \delta y = \varepsilon\eta(x)
		      \]
		      is used, but is not used in this course.
		      Note that in this notation,
		      \[
			      F[y + \delta y] = F[y] + \delta F[y];\quad \delta F[y] = \int_\alpha^\beta \qty[\fdv{F[y]}{y(x)} \delta y(x)]\dd{x}
		      \]
		\item Other boundary conditions, such as \(\eval{\pdv{f}{y'}}_{\alpha, \beta}\) can be used.
		\item Note that when computing the derivatives, we regard \(x, y, y'\) as independent;
		      \[
			      \pdv{f}{y} = \eval{\pdv{f}{y}}_{x, y'}
		      \]
		      We can also compute a total derivative, for instance
		      \[
			      \dv{x} = \pdv{x} + \pdv{y}y' + \pdv{y'}y''
		      \]
		      Note that these give different results.
		      As an example, let \(f(x, y, y') = x[(y')^2 - y^2]\).
		      Then
		      \[
			      \pdv{f}{x} = (y')^2 - y^2;\quad \pdv{f}{y} = -2xy;\quad \pdv{f}{y'} = 2xy'
		      \]
		      Hence
		      \[
			      \dv{f}{x} = (y')^2 - y^2 - 2xyy' + 2xy'y''
		      \]
	\end{itemize}
\end{remark}

\subsection{First integral of Euler--Lagrange equation (eliminating \(y\))}
In some cases, we can integrate the Euler--Lagrange equation to give a first-order ordinary differential equation.
Suppose \(f\) does not explicitly depend on \(y\).
Then
\[
	\pdv{f}{y} = 0 \implies \dv{x}\qty(\pdv{f}{y'}) = 0
\]
Hence,
\[
	\pdv{f}{y'} = c;\quad c \in \mathbb R
\]
\begin{example}
	Consider geodesics on \(\mathbb R^2\); we want to find curves on which the length is minimised.
	\[
		F[y] = \int_\alpha^\beta \sqrt{\dd{x}^2 + \dd{y}^2} = \int_\alpha^\beta \underbrace{\sqrt{1 + \dv{y}{x}^2}}_{f(y')}\dd{x}
	\]
	We can apply this `first integral' form of the Euler--Lagrange equation to get
	\[
		\frac{y'}{\sqrt{1 + (y')^2}} = c
	\]
	Hence \(y'\) is a constant, so let \(y' = m\) for \(m \in \mathbb R\).
	Hence \(y = mx + c\).
\end{example}

\subsection{Geodesics on a sphere}
Consider the unit sphere \( S^2 \subset \mathbb R^3 \), and two points \( A, B \in S^2 \) which we wish to connect by a path of minimal length, where the path is constrained to the sphere.
We will parametrise the sphere with spherical polar coordinates:
\begin{align*}
	x & = \sin\theta \sin\phi \\
	y & = \sin\theta\cos\phi  \\
	z & = \cos\theta
\end{align*}
where \( \theta \in [0, \pi]; \phi \in [0, 2\pi] \).
We can calculate the length of a path using the Pythagorean theorem:
\[
	\dd{s}^2 = \dd{x}^2 + \dd{y}^2 + \dd{z}^2 = \dd{\theta}^2 + \sin^2 \theta \dd{\phi}^2
\]
We will parametrise the path by thinking of \( \phi \) as a function of \( \theta \).
This gives
\[
	\dd{s} = \sqrt{1 + \sin^2 \theta (\phi')^2} \dd{\theta}
\]
We wish to extremise the functional \( F \), given by
\[
	F[\phi] = \int_{\theta_1 = \alpha}^{\theta_2 = \beta} \dd{s} = \int_{\theta_1}^{\theta_2} \dd{s} = \sqrt{1 + \sin^2 \theta (\phi')^2} \dd{\theta}
\]
The integrand does not depend on \( \phi \) but only on its derivative; so \( \dv{f}{\phi} = 0 \).
Using the first integral form of the Euler--Lagrange equation, we have
\[
	\pdv{f}{\phi'} = k
\]
Now, we have
\begin{align*}
	\frac{\sin^2 \theta \phi'}{\sqrt{1 + \sin^2 \theta (\phi')^2}} & = k                                                                    \\
	\sin^4 \theta (\phi')^2                                        & = k^2 (1 + \sin^2 \theta (\phi')^2)                                    \\
	(\phi')^2                                                      & = \frac{k^2}{\sin^2 \theta (\sin^2 \theta - k^2)}                      \\
	\dv{\phi}{\theta}                                              & = \pm\sqrt{\frac{k^2}{\sin^2 \theta (\sin^2 \theta - k^2)}}            \\
	\phi                                                           & = \pm\int \frac{k \dd{\theta}}{\sin \theta \sqrt{\sin^2 \theta - k^2}} \\
\end{align*}
The two solutions correspond to the two directions in which we can trace the path.
We then can arrive at
\[
	\pm \frac{\sqrt{1 - k^2}}{k} \cos(\phi - \phi_0) = \cot \theta
\]
We will be able to see that this corresponds to a great circle; that is, the intersection of a plane through the origin with the sphere.
We will show later that geodesics on a sphere are \textit{only} segments of a great circle.

\subsection{First integral of Euler--Lagrange equation (eliminating \(x\))}
For any \( f (x, y, y') \), consider the quantity
\[
	\dv{x}\qty(f - y' \pdv{f}{y'})
\]
This is exactly
\begin{align*}
	\dv{x}\qty(f - y' \pdv{f}{y'}) & = \pdv{f}{x} + y' \pdv{f}{y} + y''\pdv{f}{y'} - y'' \pdv{f}{y'} - y' \dv{x}\qty(\pdv{f}{y'})                     \\
	                               & = \pdv{f}{x} + y' \pdv{f}{y} - y' \dv{x}\qty(\pdv{f}{y'})                                                        \\
	                               & = y' \underbrace{\qty( \pdv{f}{y} - \dv{x}\pdv{f}{y'} )}_{\mathclap{\text{zero by Euler--Lagrange}}} + \pdv{f}{x} \\
	                               & = \pdv{f}{x}                                                                                                     \\
\end{align*}
So, in the case that \( f \) does not depend explicitly on \( x \) (that is, \( \pdv{f}{x} \equiv 0 \)), then we have another first integral condition from the Euler--Lagrange equation:
\[
	f - y' \pdv{f}{y'} = \text{constant}
\]

\subsection{Solving the brachistochrone problem}
Consider a curve in the plane with a fixed endpoint at the origin and another fixed endpoint at \(x = \beta\).
We want to find a path such that the time taken for a particle to travel along this curve is minimised.
We previously computed that the travel time is given by
\[
	F[y] = \frac{1}{\sqrt{2g}} \int_0^\beta \frac{\sqrt{1 + (y')^2}}{\sqrt{-y}} \dd{x}
\]
This does not depend on \(x\), so we can write (ignoring the \( \frac{1}{\sqrt{2g}} \) factor)
\[
	f - y' \pdv{f}{y'} = \frac{\sqrt{1 + (y')^2}}{\sqrt{-y}} - y' \frac{y'}{\sqrt{1 + (y')^2} \sqrt{-y}} = k
\]
This gives
\begin{align*}
	\frac{1}{\sqrt{1 + (y')^2}} & = k \sqrt{-y}                                          \\
	y'                          & = \pm \frac{\sqrt{1 + k^2 y^2}}{k\sqrt{-y}}            \\
	x                           & = \pm k \int \frac{\sqrt{-y}}{\sqrt{1 + k^2 y}} \dd{y}
\end{align*}
We will parametrise further:
\[
	y = \frac{-1}{k^2} \sin^2 \frac{\theta}{2} \implies \dd{y} = \frac{-1}{k^2}\sin \frac{\theta}{2} \cos\frac{\theta}{2}
\]
Hence,
\begin{align*}
	x & = \pm k \int \frac{-1}{k^2} \frac{\sin^2 \frac{\theta}{2} \cos \frac{\theta}{2}}{\sqrt{1 - \sin^2 \frac{\theta}{2}}} \dd{\theta} \\
	  & = \mp \frac{1}{2k^2} \int (1 - \cos\theta) \dd{\theta}                                                                           \\
	  & = \mp \frac{1}{2k^2}(\theta - \sin\theta) + c
\end{align*}
The initial condition at \((0, 0)\) gives
\[
	\theta_0 = 0 \implies c = 0
\]
Taking the positive solution, we have
\begin{align*}
	x & = \frac{\theta - \sin\theta}{2k^2}       \\
	y & = \frac{-1}{k^2} \sin^2 \frac{\theta}{2}
\end{align*}
This can be shown to be a parametrised equation of a cycloid.

\subsection{Fermat's principle}
Fermat's principle states that as light travels between two points, it takes the path of least time.
Let a ray of light be represented by a path \( y(x) \).
The speed of light is given by a function \( c(x, y) \) since it depends on the material it is in.
Then the time taken is
\[
	F[y] = \int \frac{\dd{\ell}}{c} = \int_\alpha^\beta \frac{\sqrt{1 + (y')^2}}{c(x, y)} \dd{x}
\]
In this general form, \( f \) depends on \( x, y, y' \).
Now, let us assume \( c \) depends only on \( x \) and not on \( y \).
Then we can use a first integral form to get
\[
	\pdv{f}{y'} = \text{constant}
\]
This gives
\[
	\frac{y'}{c(x)\sqrt{1 + (y')^2}} = \text{constant}
\]
Suppose that at \( \alpha \), the light ray's path has an angle \( \theta_1 \) with the \( x \)-axis, and at \( \beta \) the angle is \( \theta_2 \).
Note that \( \theta_1 = \eval{\arctan y'}_{\alpha} \) and the corresponding result for \( \beta \).
Then,
\[
	\frac{\sin\theta_1}{c(x_1)} = \frac{\sin\theta}{c(x)}
\]
This is known as Snell's law.

Suppose we have a material in which \( c \) increases with \( x \).
In such a material, we then have that \( \theta \) increases with \( x \).
In a material in which \( c \) decreases as \( x \) increases, \( \theta \) naturally decreases.

Now, suppose we have a slow material with \( c = c_S \) and a fast material with \( c = c_F \) adjacent to each other.
We might like to find the path that light takes in its path between points that cross the material boundary.
Snell's law can be used to determine that the ratio between the sine of the angle and the speed of light remains constant along the light ray's path.
